package com.hhit.collaborative_learning.download;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.dao.impl.PublicDaoImplMySql;
import com.hhit.collaborative_learning.entity.Data;
import com.hhit.collaborative_learning.entity.DataComment;
import com.hhit.collaborative_learning.entity.DataCommentPage;
import com.hhit.collaborative_learning.utils.Utils;
import com.mysql.jdbc.StringUtils;

public class LoadDataCommentServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String dataId=request.getParameter("dataId");
		String currentPage=request.getParameter("currentPage");
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ path + "/";
		PublicDaoImplMySql dao=new PublicDaoImplMySql();
		DataCommentPage page=new DataCommentPage();
		page=dao.loadDataComment(Integer.parseInt(dataId), currentPage);
		String html="<dl class='recom_list'>";
		for(int i=0;i<page.list.size();i++){
			DataComment dc=page.list.get(i);
			html+="<dt><span class='fr'>&nbsp;&nbsp;"+dc.comTime+"</span> <img src='"+basePath+"userIcons/"+dc.comUserIcon+"' alt=''><a href='/user/u013729963' class='user_name'>"+dc.comUserName+"</a></dt>";
			html+="<dd>"+dc.comContent+"<div class='wrap-rep clearfix'></div></dd>";
		}
		html+="</dl>";
		if(page.totalPage>1){
			html+="<div class='page_nav'>共"+page.count+"个  &nbsp; 共"+page.totalPage+"页&nbsp;";
			if(page.currentPage>1){
				html+="<a class='pageliststy' href='javascript:CC_Comment.getContent("+(page.currentPage-1)+")'>下一页</a>&nbsp;";
			}
			for(int i=1;i<=page.totalPage;i++){
				if(page.currentPage==i){
					html+="<strong>"+i+"</strong>&nbsp;";
				}else{
					html+="<a class='pageliststy' href='javascript:CC_Comment.getContent("+i+")'>"+i+"</a>&nbsp;";
				}
			}
			if(page.currentPage<page.totalPage){
				html+="<a class='pageliststy' href='javascript:CC_Comment.getContent("+(page.currentPage+1)+")'>下一页</a>&nbsp;";
			}
			html+="</div>";
		}
		String userId=Utils.getCookieValue("userId", request);
		if(StringUtils.isNullOrEmpty(userId)){
			html+="<dl class='cant'><dt>您不能发表评论，可能是以下原因</dt><dd>您还没有登陆</dd></dl>";
			html+="<dl class='cant cc_comment_msg' style='display: none;'><dt>&nbsp;</dt><dd></dd></dl>";
		}else{
			Data data=dao.getDataById(Integer.parseInt(dataId));
			if(userId.equals(""+data.dataUId)){
				html+="<dl class='cant'><dt>您不能发表评论，可能是以下原因</dt><dd>您不能对自己的资源评论</dd></dl>";
				html+="<dl class='cant cc_comment_msg' style='display: none;'><dt>&nbsp;</dt><dd></dd></dl>";
			}else{
				String id=dao.getUserDownload(Integer.parseInt(userId));
				String[] ids=id.split(",");
				if(dao.isContain(ids, dataId)){
					if(dao.isDownloaded(Integer.parseInt(dataId), Integer.parseInt(userId))){
						html+="<dl class='cant'><dt>您不能发表评论，可能是以下原因</dt><dd>您已经评论过</dd></dl>";
						html+="<dl class='cant cc_comment_msg' style='display: none;'><dt>&nbsp;</dt><dd></dd></dl>";
					}else{
						html+="<div class='cc_comment_form recom_sub'>";
						html+="<form>";
						html+="<table border='0' cellspacing='0' cellpadding='0'>";
						html+="<colgroup><col width='40'><col></colgroup>";
						html+="<tbody>";
						html+="<tr><th colspan='2'><textarea class='reply-content' id='cc_body' cols='' rows='' onfocus='if (this.value=='不少于5个字') this.value='';'></textarea></th></tr>";
						html+="<tr><th colspan='2' class='tr'><label class='red' id='cc_body_msg'></label><div class='mess'>评论内容不能少于5个字 </div><input type='button' value='发表评论' class='btn btn-primary' onclick='CC_Comment.postComment();'></th></tr>";
						html+="</tbody></table></form></div>";
						html+="<dl class='cant cc_comment_msg' style='display: none;'><dt>&nbsp;</dt><dd></dd></dl>";
					}
				}else{
					html+="<dl class='cant'><dt>您不能发表评论，可能是以下原因</dt><dd>您还没有下载该资源</dd></dl>";
					html+="<dl class='cant cc_comment_msg' style='display: none;'><dt>&nbsp;</dt><dd></dd></dl>";
				}
			}
		}
		response.getWriter().print(html);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
}
/*
 * <dl class="cant">
	<dt>您不能发表评论，可能是以下原因</dt>
	<dd>您可能没有下载过该资源
	</dd>
</dl>
 <dl class="cant cc_comment_msg" style="display: none;">
	<dt>&nbsp;</dt><dd></dd></dl> 
	
<div class="page_nav">共25个  &nbsp; 共3页&nbsp;
<strong>1</strong>&nbsp;
<a class="pageliststy" href="javascript:CC_Comment.getContent(2)">2</a>&nbsp;
<a class="pageliststy" href="javascript:CC_Comment.getContent(3)">3</a>&nbsp;
<a class="pageliststy" href="javascript:CC_Comment.getContent(2)">下一页</a>&nbsp;
</div>


<div class="cc_comment_form recom_sub">
<form>
<table border="0" cellspacing="0" cellpadding="0">
	<colgroup><col width="40"><col></colgroup>
	<tbody>
	<tr>
		<th>评论：</th>
		<td><span class="gray_1  col-xs-7">一个资源可评论一次，评论通过审核后，返回下载消耗积分.</span><a class="help" href="/help">查看评论审核标准</a></td>
	</tr>
	<tr><th colspan="2"><textarea class="reply-content" id="cc_body" cols="" rows="" onfocus="if (this.value=='不少于5个字') this.value='';"></textarea></th></tr>
	<tr><th colspan="2" class="tr"><label class="red" id="cc_body_msg"></label><div class="mess">评论内容不能少于5个字 </div><input type="button" value="发表评论" class="btn btn-primary" onclick="CC_Comment.postComment();"></th></tr>
</tbody></table></form></div>
 * */
